package com.example.tianwu.daydayup;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.widget.ImageView;

/**
 * Created by Tianwu on 2015/11/19.
 */
public class DrawChart {

    private static int chartHeigth = 600;
    private static int chartWidth = 600;
    private static int toLR = 50;
    private static int toTB = 50;

    private static int[] colorR ={10,255,10,255,255,200,100,200,100,50,255};
    private static int[] colorG ={200,65,10,112,96,50,100,100,200,255,255};
    private static int[] colorB ={10,60,200,203,23,200,100,100,200,80,0};

    private static Point[] getLinePoint(float[] data){
        Point[] points = new Point[(int)data[0]];
        for(int i=0;i<(int)data[0];i++) {
            points[i] = new Point(toLR + i * 50, (int) (chartHeigth - toTB - 5 * data[i+1]));
        }
        return points;
    }
    private static float[] getPanePoint(float[] data){
        float[] points = new float[(int)data[0]];
        for(int i=0;i<points.length;i++)
            points[i]=0;
        for(int i=0;i<(int)data[0];i++){
            points[(int)(data[i+1]/10)]++;
        }
        for(int i=0;i<points.length;i++){
            points[i]=(points[i]/(int)data[0])*360;
        }
        return points;
    }
    private static void drawLineData(Canvas canvas,Paint paint,Point[] points){
        canvas.drawPoint(points[0].x,points[0].y,paint);
        if(points.length>1)
            canvas.drawLine(points[0].x,points[0].y,points[1].x,points[1].y,paint);
        for(int i=1;i<points.length;i++){
            canvas.drawPoint(points[i].x,points[i].y,paint);
            canvas.drawLine(points[i-1].x,points[i-1].y,points[i].x,points[i].y,paint);
        }
    }
    private static void drawPaneData(Canvas canvas,Paint paint,float[] points){
        //饼图数据绘制
        RectF rf = new RectF(toLR, toTB, chartWidth - toLR, chartHeigth - toTB);
        Paint pp = new Paint(paint);
        float startAngle = 0;
        for(int i=0;i<points.length;i++){
            pp.setARGB(200,colorR[i%colorR.length],colorG[i%colorG.length],colorB[i%colorB.length]);
            canvas.drawArc(rf, startAngle,points[i],true,pp);
            startAngle+=points[i];
        }

    }

    public static void drawLineChart(ImageView imageView,float[] data){

        Bitmap bitmap = Bitmap.createBitmap(chartWidth,chartHeigth, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setStrokeWidth(3);

        drawBaseLineChart(canvas,paint);
        paint.setARGB(200,200,0,0);
        drawLineData(canvas, paint, getLinePoint(data));

        imageView.setImageBitmap(bitmap);
    }
    public static void drawPaneChart(ImageView imageView,float[] data){
        Bitmap bitmap = Bitmap.createBitmap(chartWidth,chartHeigth,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setStrokeWidth(5);

        drawBasePaneChart(canvas,paint);
        //饼图绘制

        drawPaneData(canvas,paint,getPanePoint(data));

        imageView.setImageBitmap(bitmap);
    }
    private static void drawBaseLineChart(Canvas canvas,Paint paint)
    {
        canvas.drawLine(toLR,toTB-20,toLR,chartHeigth-toTB,paint);
        canvas.drawLine(toLR,toTB-20,toLR-5/*箭头尺寸*/,toTB-10,paint);
        canvas.drawLine(toLR,toTB-20,toLR+5/*箭头尺寸*/,toTB-10,paint);
        canvas.drawLine(toLR, chartHeigth - toTB, chartWidth - toLR + 20, chartHeigth - toTB, paint);
        canvas.drawLine(chartWidth - toLR + 20, chartHeigth - toTB, chartWidth - toLR + 10, chartHeigth - toTB - 5/*箭头尺寸*/, paint);
        canvas.drawLine(chartWidth - toLR + 20, chartHeigth - toTB, chartWidth - toLR + 10, chartHeigth - toTB + 5/*箭头尺寸*/, paint);
        for(int i=0;i<10;i++){
            canvas.drawLine(toLR, toTB + i * 50, toLR - 5, toTB + i * 50, paint);
            canvas.drawText(Integer.toString(100-i*10)/*标志内容*/,toLR-40,toTB + i*50,paint);//纵坐标标志
        }
        for(int i=1;i<=10;i++){
            canvas.drawLine(toLR+i*50,chartHeigth-toTB,toLR+i*50,chartHeigth-toTB+5,paint);
            canvas.drawText(Integer.toString(i)/*标志内容*/,toLR+i*50-5,chartHeigth-toTB+15,paint);//横坐标标志
        }
        canvas.drawText("0", toLR - 5, chartHeigth - toTB + 15, paint);
        paint.setARGB(150,50,255,50);
        canvas.drawLine(toLR,toTB+200,chartWidth-toLR,toTB+200,paint);

    }
    private static void drawBasePaneChart(Canvas canvas,Paint paint){
        //饼图基础绘制
        //canvas.drawLine(toLR,toTB,chartWidth-toLR,chartHeigth-toTB,paint);
    }
}
